import sys,threading,yaml,os,tkMessageBox,tkSimpleDialog
from Tkinter import (Button,Label,StringVar,Entry,Tk,Menu,OptionMenu)

class NewCharacter (tkSimpleDialog.Dialog):

    def __init__ (self, parent):

        self.chars = {}
        self.race_options = ["Charr",
                             "Human",
                             "Asura",
                             "Norn",
                             "Sylvari"]
        self.class_options = ["Warrior",
                              "Guardian",
                              "Ranger",
                              "Thief",
                              "Necromancer",
                              "Elementalist",
                              "Mesmer",
                              "Engineer"]

        tkSimpleDialog.Dialog.__init__(self, parent,  "New Character Profile")

    def body (self, root):

        # Name
        Label(root, text="Character Name").grid(row=1, padx=5, pady=5)
        self.entry_name = Entry(root)
        self.entry_name.grid(row=1, column=1, padx=5, pady=5)
        # Race
        Label(root, text="Race").grid(row=2, padx=5, pady=5)
        self.race = StringVar()
        OptionMenu(root, self.race, *self.race_options).grid(
            row=2, column=1, padx=5, pady=5)
        # Class
        Label(root, text="Class").grid(row=3, padx=5, pady=5)
        self.char_class = StringVar()
        OptionMenu(root, self.char_class, *self.class_options).grid(
            row=3, column=1, padx=5, pady=5)

    def validate (self):

        if not self.entry_name.get():
            ERROR("No Name!",
                "You haven't entered your character's name!")
            return False
        if not self.race.get():
            ERROR("No Race!",
                "Please enter your race.")
            return False

        return True

    def apply (self):

        self.chars[self.entry_name.get()] = {
            "race": self.race.get().strip(),
            "class": self.char_class.get().strip()
           }
        print self.chars
        if not os.path.exists(".profiles/"):
            os.mkdir(".profiles")
        for profile in self.chars.keys():
            f = open(os.path.join(".profiles", profile+".yml"), "w")
            yaml.dump(self.chars[profile], f)
